package com.agriculture.monitoring.repository;

import com.agriculture.monitoring.entity.GeoDevicePath;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface DevicePathRepository extends JpaRepository<GeoDevicePath, Long> {
    
    List<GeoDevicePath> findByDeviceIdOrderByStartTimeDesc(Long deviceId);
    
    @Query("SELECT p FROM GeoDevicePath p WHERE p.device.id = :deviceId AND p.startTime >= :startTime AND p.endTime <= :endTime ORDER BY p.startTime")
    List<GeoDevicePath> findByDeviceIdAndTimeRange(@Param("deviceId") Long deviceId, 
                                                  @Param("startTime") LocalDateTime startTime, 
                                                  @Param("endTime") LocalDateTime endTime);
    
    @Query(value = "SELECT * FROM geo_device_path WHERE device_id = :deviceId AND " +
            "DATE(start_time) = DATE(:date) ORDER BY start_time", nativeQuery = true)
    List<GeoDevicePath> findByDeviceIdAndDate(@Param("deviceId") Long deviceId, 
                                             @Param("date") String date);
}